package com.itown.expo.service;

import java.util.ArrayList;
import java.util.List;

import webframe.common.file.FileTransferHelper;

import com.itown.expo.model.DeclGoodsImg;
import com.itown.expo.model.Goods;

public class FileBizService extends MyBaseService {
	public void deleteGoodsImgFiles(List<Long> deleteFileId, String imgPath,
			String previewPath) {
		List<DeclGoodsImg> list = new ArrayList<DeclGoodsImg>();
		for (Long id : deleteFileId) {
			list.add(getDeclGoodsImgDao().get(id));
		}
		for (DeclGoodsImg img : list) {
			getDeclGoodsImgDao().delete(img);
			if (imgPath != null) {
				FileTransferHelper.deleteFile(img.getFileName(), imgPath);
			}
			if (previewPath != null) {
				FileTransferHelper.deleteFile(img.getPreviewImgName(),
						previewPath);
			}
		}
	}

	public void saveGoodsImgFiles(List<DeclGoodsImg> files) {
		for (DeclGoodsImg declGoodsImg : files) {
			getDeclGoodsImgDao().save(declGoodsImg);
		}
	}

	public List<DeclGoodsImg> findGoodsImgListByGoodsId(Long id) {
		return getDeclGoodsImgDao().findByHql(
				"from DeclGoodsImg t where t.goodsId=? order by t.id",
				new Object[] { id });
	}

	public Goods findGoodsById(Long id) {
		return getGoodsDao().get(id);
	}
}
